package net.xxcxcxcx.xc_enhancedmob.render.particle.summon.draw;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.api.EnvironmentInterface;
import net.fabricmc.api.EnvironmentInterfaces;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.util.math.Vec3d;
import net.xxcxcxcx.xc_enhancedmob.render.particle.ParticleHelper;
import net.xxcxcxcx.xc_enhancedmob.render.particle.summon.BaseSummonParticle;

@EnvironmentInterfaces(value = {@EnvironmentInterface(value = EnvType.CLIENT, itf = ClientWorld.class)})
public class DrawCircleSummonParticle extends BaseSummonParticle {
    //执行了k次，总共执行n次，同步执行m次。总共把圆分成n*m份
    //r是半径
    public int k = 0, n = 20, m = 3, r = 2;

    public DrawCircleSummonParticle() {
        super();
    }

    public DrawCircleSummonParticle(long t, Vec3d pos, int rr) {
        this(t, pos);
        r = rr;
    }

    public DrawCircleSummonParticle(long t, Vec3d pos, int kk, int nn, int mm, int rr) {
        this(t, pos, rr);
        k = kk;
        n = nn;
        m = mm;
    }

    public DrawCircleSummonParticle(long t, Vec3d pos) {
        super(t, pos);
    }

    public DrawCircleSummonParticle(PacketByteBuf buf) {
        read(buf);
    }

    @Override
    public void read(PacketByteBuf buf) {
        super.read(buf);
        k = buf.readInt();
        n = buf.readInt();
        m = buf.readInt();
        r = buf.readInt();
    }

    //文件保存为NBT
    @Override
    public void write(PacketByteBuf buf) {
        super.write(buf);
        buf.writeInt(k);
        buf.writeInt(n);
        buf.writeInt(m);
        buf.writeInt(r);
    }

    //每秒中要执行的粒子效果
    @Override
    @Environment(value = EnvType.CLIENT)
    public void Dotick(ClientWorld clientWorld) {
        for (int i = 0; i < m; i++) {
            double radians = Math.toRadians(360.0 / (n * m) * (i * n + k));
            Vec3d pos = new Vec3d(this.pos.getX() + Math.sin(radians) * r, this.pos.getY(), this.pos.getZ() + Math.cos(radians) * r);
            ParticleHelper.spawnlistParticle(ParticleTypes.END_ROD, pos.x, pos.y, pos.z, 0, 0.05, 0);
        }
        k++;
        if (k == n) k = 0;
    }
}
//ParticleTypes.FIREWORK
//ParticleTypes.END_ROD